package com.fortune.cat.resource;

import com.fortune.cat.bean.RouteDefinitionRecipient;
import com.fortune.cat.service.RouteDefinitionService;
import com.fortune.cat.vo.FortuneCatResult;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Mono;

import java.util.List;

/**
 * @author zhaozeyang
 * @date 2019/4/17 17:30
 * @description
 */
@RestController
@RequestMapping("/fortune/cat/route")
public class RouteDefinitionResource {


    private final RouteDefinitionService routeDefinitionService;

    public RouteDefinitionResource(RouteDefinitionService routeDefinitionService) {
        this.routeDefinitionService = routeDefinitionService;
    }

    @PostMapping("")
    public Mono<FortuneCatResult> createRoute(@RequestBody RouteDefinitionRecipient routeDefinition) {
        try {
            RouteDefinitionRecipient routeDefinitionRecipient = routeDefinitionService.createOrUpdate(routeDefinition);
            return Mono.just(FortuneCatResult.success(FortuneCatResult.success("create route success", routeDefinitionRecipient)));
        } catch (Exception e) {
            return Mono.just(FortuneCatResult.success(FortuneCatResult.error("create route exception")));
        }
    }

    @GetMapping("")
    public Mono<FortuneCatResult> getRoute(@RequestParam(value = "routeName", required = false, defaultValue = "") String routeName,
                                           @RequestParam(value = "page", required = false, defaultValue = "0") Integer page,
                                           @RequestParam(value = "number", required = false, defaultValue = "10") Integer number) {
        try {
            List<RouteDefinitionRecipient> routeDefinitionRecipient = routeDefinitionService.getRoute(routeName,page,number);
            return Mono.just(FortuneCatResult.success(FortuneCatResult.success("create route success", routeDefinitionRecipient)));
        } catch (Exception e) {
            return Mono.just(FortuneCatResult.success(FortuneCatResult.error("create route exception")));
        }
    }

    @GetMapping("/{id}")
    public Mono<FortuneCatResult> getRoute(@PathVariable(value = "id") String routeName) {
        try {
            RouteDefinitionRecipient routeDefinitionRecipient = routeDefinitionService.getRoute(routeName);
            return Mono.just(FortuneCatResult.success(FortuneCatResult.success("create route success", routeDefinitionRecipient)));
        } catch (Exception e) {
            return Mono.just(FortuneCatResult.success(FortuneCatResult.error("create route exception")));
        }
    }

    @PutMapping("")
    public Mono<FortuneCatResult> updateRoute(@RequestBody RouteDefinitionRecipient routeDefinition) {
        try {
            RouteDefinitionRecipient routeDefinitionRecipient = routeDefinitionService.createOrUpdate(routeDefinition);
            return Mono.just(FortuneCatResult.success(FortuneCatResult.success("create route success", routeDefinitionRecipient)));
        } catch (Exception e) {
            return Mono.create(fortuneCatResult -> FortuneCatResult.success(FortuneCatResult.error("create route exception")));
        }
    }

    @DeleteMapping("/{id}")
    public Mono<FortuneCatResult> deleteRoute(@PathVariable("id") Long id) {
        try {
            routeDefinitionService.deleteRoute(id);
            return Mono.just(FortuneCatResult.success(FortuneCatResult.success("delete route success")));
        } catch (Exception e) {
            return Mono.create(fortuneCatResult -> FortuneCatResult.success(FortuneCatResult.error("create route exception")));
        }
    }
}
